window.onload = function (e) {
    e = e || window.event;
    e.returnValue = false;


    //获取元素
    let login_box = document.querySelector(".login_box");
    let even = document.querySelector(".even");
    let center_right = document.querySelector(".center_right");
    let car = document.querySelector(".car");
    let phoneEmail = document.querySelector(".phoneEmail");
    let pass = document.querySelector(".pass");
    let phoneError = document.querySelector(".phoneError");
    let passError = document.querySelector(".passError");
    let btn_login = document.querySelector(".btn_login");
    let loginForm = document.querySelector(".login");

    login_box.onclick = function (e) {
        if (e.target.classList.contains("find")) {
            center_right.style.right = 0;
            even.style.display = 'block';
        }

        if (e.target.classList.contains("cancle")) {
            center_right.style.right = "-511px";
            even.style.display = 'none';
        }

        if (e.target.classList.contains("shoppingcar")) {
            car.style.right = 0;
            even.style.display = 'block';
        }

        if (e.target.classList.contains("quxiao")) {
            car.style.right = "-511px";
            even.style.display = 'none';

        }
    }

    //登录正则验证(电话号码或邮箱)
    phoneEmail.oninput = function () {
        let pNumreg = /^1[3-9]\d{9}$/i;
        let emailreg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        let pNumberRes = pNumreg.test(phoneEmail.value);
        let emailRes = emailreg.test(phoneEmail.value);
        if (phoneEmail.value.length == 0) {
            phoneError.innerHTML = "请输入手机号码或者电子邮箱";
            return;
        }
        if (!pNumberRes && !emailRes) {
            phoneError.innerHTML = "请输入正确的手机号码或电子邮箱";
            return;
        }
        phoneError.innerHTML = " ";
    }

    //密码
    pass.oninput = function () {
        if (pass.value.length < 1) {
            passError.innerHTML = "请输入密码";
            return;
        }
        passError.innerHTML = " ";
    }

    loginForm.onsubmit = function (e) {
        e = e || window.event;
        e.returnValue = false;

        let d = pAjax({
            url: "/api/login",
            type: "get",
            data: {
                phone: phoneEmail.value,
                email: phoneEmail.value,
                password: pass.value
            }
        });
        d.then((res) => {
            res = JSON.parse(res);
            if (res.code) {
                setCookie("login", phoneEmail.value);
                let url = localStorage.getItem("url");
                localStorage.removeItem("url");
                location.href = url ? url : "../html/index.html";
                alert(res.msg);
                return;
            }
            alert(res.msg);
            phoneEmail.value = "";
            pass.value = "";
        })

    }
}